package com.tanhua.app.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.UserHolder;
import com.tanhua.api.db.BlackListApi;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.SettingsApi;
import com.tanhua.db.Question;
import com.tanhua.db.Settings;
import com.tanhua.db.User;
import com.tanhua.vo.PageBeanVo;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;

    public ResponseEntity findSettings() {
        // 获取当前用户
        User user = UserHolder.getUser();
        // 根据用户id查询问题
        Question question = questionApi.findQuestion(user.getId());
        // 根据用户id查询通知
        Settings settings = settingsApi.findSettings(user.getId());
        // 合成app需要的vo进行返回
        SettingsVo vo = new SettingsVo();
        vo.setId(user.getId());
        vo.setPhone(user.getPhone());
        if(question!=null){
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if(settings!=null){
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity updateQuestions(String content) {

        // 获取当前用户
        User user = UserHolder.getUser();
        // 根据id做查询
        Question question = questionApi.findQuestion(user.getId());
        // 判断
        if(question==null){
            // 没有 新增
            question=new Question();
            question.setUserId(user.getId());
            question.setStrangerQuestion(content);
            questionApi.saveQuestion(question);
        }else{
            // 有  修改
            question.setStrangerQuestion(content);
            questionApi.updateQuestion(question);
        }

        return ResponseEntity.ok(null);

    }

    public ResponseEntity updateSetting(Settings settings) {
        // 获取当前用户
        User user = UserHolder.getUser();
        // 获取通知
        Settings st = settingsApi.findSettings(user.getId());
        //判断
        if(st!=null){
            st.setLikeNotification(settings.getLikeNotification());
            st.setPinglunNotification(settings.getPinglunNotification());
            st.setGonggaoNotification(settings.getGonggaoNotification());
            //修改
            settingsApi.updateSetting(st);
        }else{
            //新增
            settings.setUserId(user.getId());
            settingsApi.saveSetting(settings);
        }

        return ResponseEntity.ok(null);
    }



    public ResponseEntity findBlackList(Integer page, Integer pagesize) {

        // 获取当前用户
        User user = UserHolder.getUser();
        // 根据当前用户的id查询黑名单用户
        IPage ipage = blackListApi.findBlackList(user.getId(), page, pagesize);
        // 组装app需要的vo
        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,ipage.getTotal(),ipage.getRecords());
        // 返回vo给app
        return ResponseEntity.ok(pageBeanVo);

    }

    public ResponseEntity removeBlackList(String blackid) {
        Long userid = UserHolder.getUser().getId();
        blackListApi.removeBlackList(userid,blackid);
        return ResponseEntity.ok(null);
    }
}
